package com.msc.sdm.app.view.workspace;

import com.msc.sdm.application.Core;
import com.msc.sdm.config.Configuration;
import com.msc.sdm.generic.utils.I18NL10NManager;
import com.msc.sdm.ic.pub.SimMgrClientConnection;
import com.msc.sdm.ic.pub.SimMgrConnection;
import com.msc.sdm.ic.pub.SimMgrDbObject;
import com.msc.sdm.ic.pub.SimMgrDbObjectList;
import com.msc.sdm.ic.pub.SimMgrProcess;
import com.msc.sdm.ic.pub.SimMgrQuery;
import com.msc.sdm.ic.pub.SimMgrValue;
import com.msc.sdm.ic.pub.exception.SimMgrUnknownPropertyException;
import com.msc.sdm.ic.pub.inproc.SimMgrProcessInputMapImpl;
import com.msc.sdm.ic.pub.schema.SimMgrObjectType;
import com.msc.sdm.ui.widget.Container;
import com.msc.sdm.ui.widget.Dialog;
import com.msc.sdm.ui.widget.UILayout;
import com.msc.sdm.ui.widget.Widget;
import com.msc.sdm.ui.widget.event.ActionEvent;
import com.msc.sdm.ui.widget.event.ActionListener;
import com.msc.sdm.ui.widget.event.Event;
import com.msc.sdm.ui.widget.event.EventContext;
import com.msc.sdm.ui.widget.toolkit.core.event.ActionEventImpl;
import com.msc.sdm.ui.widget.toolkit.sm.NavigationTreeWidget;
import com.msc.sdm.ui.widget.toolkit.sm.SMActionInfo;
import com.msc.sdm.ui.widget.toolkit.sm.SMActionUILayout;
import com.msc.sdm.ui.widget.toolkit.sm.SMBasicWorkspace;
import com.msc.sdm.ui.widget.toolkit.sm.SMPlainWorkspace;
import com.msc.sdm.ui.widget.toolkit.sm.SMWorkspace;
import com.msc.sdm.ui.widget.toolkit.sm.SMWorkspaceManager;
import com.msc.sdm.ui.widget.toolkit.sm.SMWorkspaceManagerImpl;
import com.msc.sdm.ui.widget.toolkit.sm.SimMgrWidgetFactory;
import com.msc.sdm.ui.widget.toolkit.sm.bc.impl.BreadcrumbUtils;
import com.msc.sdm.ui.widget.toolkit.sm.mod.view.impl.Tools;
import com.msc.sdm.ui.widget.toolkit.sm.tree.model.impl.NavigationTree;
import com.msc.sdm.ui.widget.util.CoreWidgetsUtils;
import com.msc.sdm.ui.widget.util.UIContext;
import com.msc.sdm.ui.widget.util.UIContextServiceRegistry;
import com.msc.sdm.web.controller.rest.Util;
import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;

public class NavigateWorkspaceView
  extends WorkspaceView
{
  private static final long serialVersionUID = 1L;
  
  public void init(EventContext paramEventContext)
  {
    setUILayout(paramEventContext.getUILayout());
    setStateVariables(null, null, null);
    ((SMPlainWorkspace)getUILayout()).setWorkAreaWidgetName("workAreaPanel");
  }
  
  private void setStateVariables(SimMgrQuery paramSimMgrQuery, String paramString1, String paramString2)
  {
    getUILayout().setValue("currentQuery", paramSimMgrQuery);
    getUILayout().setValue("currentTitle", paramString1);
    getUILayout().setValue("objectDisplay", paramString2);
  }
  
  public void handleTreeNodeClick(EventContext paramEventContext)
    throws Exception
  {
    setUILayout(paramEventContext.getUILayout());
    SimMgrQuery localSimMgrQuery = (SimMgrQuery)paramEventContext.getEvent().getEventData("value");
    SimMgrObjectType localSimMgrObjectType = localSimMgrQuery.getObjectType();
    if (localSimMgrObjectType.isDerivedFrom("ObjectFolder"))
    {
      showFolderItemsForSelectedFolder(localSimMgrQuery);
    }
    else
    {
      setStateVariables(localSimMgrQuery, null, "SOD");
      ((SMPlainWorkspace)getUILayout()).showSOD(localSimMgrQuery);
    }
    Widget localWidget = getUILayout().getChildWidget("workAreaPanel");
    localWidget.setValue("reRender", "true");
  }
  
  private void showFolderItemsForSelectedFolder(SimMgrQuery paramSimMgrQuery)
    throws Exception
  {
    SimMgrQuery localSimMgrQuery = paramSimMgrQuery;
    if (Core.getConfig().isShowNavigateFolderContents()) {
    	if( paramSimMgrQuery.getObjectType().isDerivedFrom("TestDataFolder") ) {
            String projectName = paramSimMgrQuery.evaluate("project.name[0]").getString();
            if(projectName.startsWith("/EDS")){
              localSimMgrQuery = paramSimMgrQuery.navigate("folderItems.object:EDSTestData");
            }else{
              localSimMgrQuery = paramSimMgrQuery.navigate("folderItems.object:NIOTestData");
            }
    	}else {
    		localSimMgrQuery = paramSimMgrQuery.navigate("folderItems.object");
    	}
    }
    SimMgrDbObject localSimMgrDbObject = paramSimMgrQuery.retrieveObjects().get(0);
    String str = getUIContext().getI18NL10NManager().getMsg("workspace.navigate.folderMOD.title", new Object[] { localSimMgrDbObject.evaluate("name").getString() });
    if (localSimMgrQuery.size() == 0)
    {
      localSimMgrQuery = paramSimMgrQuery;
      str = null;
    }
    showMODOfObjects(localSimMgrQuery, str);
  }
  
  private void showMODOfObjects(SimMgrQuery paramSimMgrQuery, String paramString)
    throws Exception
  {
    if ((paramSimMgrQuery.retrieveObjects().size() == 1) && (isShowSODInNavigateWorkspace()))
    {
      setStateVariables(paramSimMgrQuery, null, "SOD");
      ((SMBasicWorkspace)getUILayout()).showSOD(paramSimMgrQuery);
    }
    else
    {
      LinkedHashMap localLinkedHashMap = new LinkedHashMap();
      localLinkedHashMap.put("styleClass", "WorkAreaScroll");
      
      String str1 = (String)getUILayout().getValue("CurrentMODWidgetName");
      if (str1 != null)
      {
    	Container localObject = (Container) getWidget("workAreaPanel");
        Widget localWidget1 = localObject.getChildWidget(str1);
        if (localWidget1 != null)
        {
          Widget localWidget2 = (Widget)localWidget1.getValue("modDataExtractor");
          if (localWidget2 != null)
          {
            String str2 = (String)localWidget2.getValue("pageSize");
            localLinkedHashMap.put("pageSize", str2);
          }
        }
      }
      Object localObject = getWidgetFactory().createMODWidget(paramSimMgrQuery, getUIContext(), localLinkedHashMap);
      
      getUILayout().setValue("CurrentMODWidgetName", ((Widget)localObject).getWidgetName());
      
      setStateVariables(paramSimMgrQuery, paramString, "MOD");
      ((Widget)localObject).setValue("displayMode", "Tabbed");
      ((Widget)localObject).setValue("expandWidth", Boolean.valueOf(true));
      if (paramString != null) {
        ((Widget)localObject).setValue("title", paramString);
      }
      ((SMBasicWorkspace)getUILayout()).showWidget((Widget)localObject);
    }
  }
  
  private boolean isShowSODInNavigateWorkspace()
  {
    SimMgrConnection localSimMgrConnection = (SimMgrConnection)getUIContext().getValue("connection");
    try
    {
      String str = localSimMgrConnection.getSystemProperty("navigationWS.show.SOD");
      if ((str != null) && (str.equals("true"))) {
        return true;
      }
      return false;
    }
    catch (Exception localException) {}
    return false;
  }
  
  public void handleTreeNodeObjectsClick(EventContext paramEventContext)
    throws Exception
  {
    setUILayout(paramEventContext.getUILayout());
    SimMgrQuery localSimMgrQuery = (SimMgrQuery)paramEventContext.getEvent().getEventData("value");
    showMODOfObjects(localSimMgrQuery, null);
    Widget localWidget = getUILayout().getChildWidget("workAreaPanel");
    localWidget.setValue("reRender", "true");
  }
  
  public void handleMODActionClick(EventContext paramEventContext)
    throws Exception
  {
    setUILayout(paramEventContext.getUILayout());
    Dialog localDialog = (Dialog)getWidget("navActionDialog");
    clearContainer(localDialog);
    SMActionInfo localSMActionInfo = (SMActionInfo)paramEventContext.getEvent().getEventData("smActionInfo");
    
    SMActionUILayout localSMActionUILayout = null;
    if (localSMActionInfo == null) {
      return;
    }
    if (isDisplayInPIPTab(localSMActionInfo.getActionName()))
    {
      launchPIPWorkspace(paramEventContext.getEvent());
    }
    else
    {
      localSMActionUILayout = getWidgetFactory().createActionUILayout(getUIContext(), localSMActionInfo);
      if (localSMActionUILayout != null)
      {
        localSMActionUILayout.setValue("showBorder", "false");
        localSMActionUILayout.setValue("viewName", localSMActionInfo.getViewName());
        localDialog.addChildWidget(localSMActionUILayout);
        String str = localSMActionInfo.getDisplayLabel();
        if (str == null) {
          str = (String)localSMActionUILayout.getValue("title");
        }
        localDialog.setValue("title", str);
        localSMActionUILayout.showPage();
        SimMgrProcessInputMapImpl localSimMgrProcessInputMapImpl = (SimMgrProcessInputMapImpl)localSMActionUILayout.getContext().getProcessInputMap();
        boolean flg = localSimMgrProcessInputMapImpl != null ? localSimMgrProcessInputMapImpl.isProcessDiscardable() : false;
        if ( flg ) {
          localSMActionUILayout.addScriptListener("postSubmit", "handlePostSubmitForDiscard");
        }
        boolean bool = isShowUI(localSMActionInfo.getActionName());
        if (bool == true)
        {
          localDialog.showDialog();
        }
        else if ((localSMActionInfo.getActionName().equals("RemoveFromClipboardSimActivity")) || (localSMActionInfo.getActionName().equals("ClearClipboardSimActivity")))
        {
          refreshNavigationTree();
          refreshWorkArea();
        }
      }
    }
  }
  
  private void refreshNavigationTree()
    throws Exception
  {
    NavigationTreeWidget localNavigationTreeWidget = (NavigationTreeWidget)getUILayout().getWidget("navigationTree", true);
    localNavigationTreeWidget.getTreeWidget().refreshCurrentTree();
    
    Widget localWidget = getUILayout().getChildWidget("navigationTree");
    localWidget.setValue("reRender", "true");
  }
  
  private void refreshWorkArea()
    throws Exception
  {
    SimMgrQuery localSimMgrQuery = (SimMgrQuery)getUILayout().getValue("currentQuery");
    if (localSimMgrQuery != null)
    {
    	String localObject = (String)getUILayout().getValue("objectDisplay");
      if ( localObject.equals("SOD") )
      {
        ((SMBasicWorkspace)getUILayout()).showSOD(localSimMgrQuery);
      }
      else
      {
        String str = (String)getUILayout().getValue("currentTitle");
        showMODOfObjects(localSimMgrQuery, str);
      }
    }
    Object localObject = getUILayout().getChildWidget("workAreaPanel");
    ((Widget)localObject).setValue("reRender", "true");
  }
  
  public void handleMultiObjectClick(EventContext paramEventContext)
    throws Exception
  {
    setUILayout(paramEventContext.getUILayout());
    SimMgrQuery localSimMgrQuery = (SimMgrQuery)paramEventContext.getEvent().getEventData("value");
    showMODOfObjects(localSimMgrQuery, null);
  }
  
  public void handleTreeRefresh(EventContext paramEventContext)
  {
    setUILayout(paramEventContext.getUILayout());
    getUILayout().setValue("errorMessages", "");
  }
  
  public void handlePostSubmit(EventContext paramEventContext)
    throws Exception
  {
    setUILayout(paramEventContext.getUILayout());
    clearAndCloseDialog("navActionDialog");
    SimMgrProcess localSimMgrProcess = (SimMgrProcess)paramEventContext.getEvent().getEventData("process");
    String str = localSimMgrProcess.getOutputBehavior();
    Object localObject1;
    Object localObject2;
    if ((str == null) || (str.equals("navigate")))
    {
      localObject1 = null;
      if (getLandingPage().isPresent()) {
        localObject1 = (String)getLandingPage().get();
      }
      localObject2 = (SMWorkspaceManagerImpl)BreadcrumbUtils.findWorkspaceManager(getUILayout());
      if ((null != localObject1) && (((String)localObject1).equals("none"))) {
        ((SMWorkspaceManagerImpl)localObject2).setValue("DefaultWorkspace", "navigate");
      } else {
        ((SMWorkspaceManagerImpl)localObject2).setValue("DefaultWorkspace", (Serializable)localObject1);
      }
      handlePostSubmitCommon(paramEventContext);
      ((CoreWidgetsUtils)getUIContext().getServiceRegistry().getObject("coreWidgetsUtils")).nwsReallyRefreshNavigationTree(getUILayout());
    }
    else if (str.equals("homepage"))
    {
      localObject1 = getUILayout().getRootWidget(getUILayout());
      localObject2 = (SMWorkspaceManager)((Container)localObject1).getChildWidget("wm");
      ((SMWorkspaceManager)localObject2).activateTab("myWorkspace");
      SMWorkspace localSMWorkspace = ((SMWorkspaceManager)localObject2).getWorkspaceByName("myWorkspace");
      localSMWorkspace.call("showHomeWidget");
    }
    else if (str.equals("none"))
    {
      ((CoreWidgetsUtils)getUIContext().getServiceRegistry().getObject("coreWidgetsUtils")).nwsReallyRefreshNavigationTree(getUILayout());
      handleActivateNavigateTab(paramEventContext);
      
      CoreWidgetsUtils.refreshNavigateWorkArea(getUILayout());
    }
  }
  
  public void handleActivateNavigateTab(EventContext paramEventContext)
    throws Exception
  {
    setUILayout(paramEventContext.getUILayout());
    String str = (String)getUILayout().getValue("isWorkspaceLoaded");
    if ((str == null) || (str.equals("false")))
    {
      Widget localWidget = getUILayout().getRootWidget(getUILayout());
      SMWorkspaceManager localSMWorkspaceManager = (SMWorkspaceManager)((Container)localWidget).getChildWidget("wm");
      localSMWorkspaceManager.setValue("reRender", "true");
      getUILayout().showPage();
      getUILayout().setValue("isWorkspaceLoaded", "true");
      refreshNavigationTree();
      refreshWorkArea();
    }
  }
  
  public void handleOnCancel(EventContext paramEventContext)
  {
    setUILayout(paramEventContext.getUILayout());
    clearAndCloseDialog("navActionDialog");
  }
  
  private Optional<String> getLandingPage()
  {
    try
    {
      return Optional.of(Tools.getConnection().getSystemProperty("navigateWS.action.default.output.behavior"));
    }
    catch (SimMgrUnknownPropertyException localSimMgrUnknownPropertyException) {}
    return Optional.ofNullable(null);
  }
  
  private boolean isLinkTargetNavigateWorkspace()
  {
    try
    {
      String str = Tools.getConnection().getSystemProperty("navigateWS.link.target.myWS");
      if (Util.isEmpty(str)) {
        return false;
      }
      return str.equals("false");
    }
    catch (Exception localException) {}
    return false;
  }
  
  public void handleSingleObjectSelected(EventContext paramEventContext)
    throws Exception
  {
    setUILayout(paramEventContext.getUILayout());
    
    SimMgrProcess localSimMgrProcess = (SimMgrProcess)paramEventContext.getEvent().getEventData("process");
    
    SMWorkspaceManagerImpl localSMWorkspaceManagerImpl = (SMWorkspaceManagerImpl)BreadcrumbUtils.findWorkspaceManager(getUILayout());
    if ((null == localSimMgrProcess) && (isLinkTargetNavigateWorkspace())) {
      localSMWorkspaceManagerImpl.setValue("DefaultWorkspace", "navigate");
    }
    dispatchEvent(paramEventContext, localSMWorkspaceManagerImpl, "onSingleObjectSelected");
  }
  
  public void handleMultiObjectSelected(EventContext paramEventContext)
    throws Exception
  {
    setUILayout(paramEventContext.getUILayout());
    
    SMWorkspaceManagerImpl localSMWorkspaceManagerImpl = (SMWorkspaceManagerImpl)BreadcrumbUtils.findWorkspaceManager(getUILayout());
    if (isLinkTargetNavigateWorkspace()) {
      localSMWorkspaceManagerImpl.setValue("DefaultWorkspace", "navigate");
    }
    dispatchEvent(paramEventContext, localSMWorkspaceManagerImpl, "onManyObjectsSelected");
  }
  
  private void dispatchEvent(EventContext paramEventContext, SMWorkspaceManagerImpl paramSMWorkspaceManagerImpl, String paramString)
  {
    ActionListener localActionListener = paramSMWorkspaceManagerImpl.getListenerByType(paramString);
    
    ActionEventImpl localActionEventImpl = new ActionEventImpl(paramEventContext.getEvent().getSourceWidget());
    localActionEventImpl.setType(paramString);
    localActionEventImpl.setEventData("value", paramEventContext
      .getEvent().getEventData("value"));
    localActionListener.actionPerformed(localActionEventImpl);
  }
}
